package tcp.command;

import handlers.ClientStatusHandler;
import handlers.FileDeletionHandler;
import handlers.FileMoveHandler;
import handlers.ReplicationHandler;
import handlers.StatusHandler;
import handlers.UploadRequestHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import tcp.filetransfer.DownloadHandler;
import tcp.filetransfer.FileTransferManager;

public class CommandManager extends Thread{
	
	private int portNumber;
	private ExecutorService threadPool;
	private StatusHandler statusHandler;
	private ReplicationHandler replicationHandler;
	private FileMoveHandler fileMoveHandler;
	private ClientStatusHandler clientStatusHandler;
	private FileDeletionHandler deleteFileHandler;
	private UploadRequestHandler uploadRequestHandler;
	
	public CommandManager(int portNumber) {	 
		this.portNumber = portNumber;
		this.threadPool = Executors.newCachedThreadPool();
		this.start();
	}
	private void setupListeningForDownloadRequests(){
		ServerSocket serverSocket = null;
        boolean listening = true;
        try {
			serverSocket = new ServerSocket(this.portNumber);

	        while (listening){
	        	Socket sock = serverSocket.accept();
	        	threadPool.execute(new CommandHandler(sock,this));
	        }
	        serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public ExecutorService getThreadPool() {
		return threadPool;
	}
	public StatusHandler getStatusHandler() {
		return statusHandler;
	}
	public void setStatusHandler(StatusHandler statusHandler) {
		this.statusHandler = statusHandler;
	}
	public ReplicationHandler getReplicationHandler() {
		return replicationHandler;
	}
	public void setReplicationHandler(ReplicationHandler replicationHandler) {
		this.replicationHandler = replicationHandler;
	}
	public FileMoveHandler getFileMoveHandler() {
		return fileMoveHandler;
	}
	public void setFileMoveHandler(FileMoveHandler fileMoveHandler) {
		this.fileMoveHandler = fileMoveHandler;
	}
	public ClientStatusHandler getClientStatusHandler() {
		return clientStatusHandler;
	}
	public void setClientStatusHandler(ClientStatusHandler clientStatusHandler) {
		this.clientStatusHandler = clientStatusHandler;
	}
	public FileDeletionHandler getDeleteFileHandler() {
		return deleteFileHandler;
	}
	public void setDeleteFileHandler(FileDeletionHandler deleteFileHandler) {
		this.deleteFileHandler = deleteFileHandler;
	}
	public UploadRequestHandler getUploadRequestHandler() {
		return uploadRequestHandler;
	}
	public void setUploadRequestHandler(UploadRequestHandler uploadRequestHandler) {
		this.uploadRequestHandler = uploadRequestHandler;
	}
	@Override
	public void run(){
		this.setupListeningForDownloadRequests();
	}
	
}
